local CommonOHOH = _G.CommonOHOH;

local PlayerInfo={}
local CallBackList={}
local UpdateConnect=nil
game.Players.PlayerAdded:Connect(function(player)
	if not PlayerInfo[player.UserId] then
		local headShot, isReady = game.Players:GetUserThumbnailAsync(player.UserId, Enum.ThumbnailType.HeadShot,Enum.ThumbnailSize.Size60x60)
		if  headShot then
			PlayerInfo[player.UserId]={
				Head=headShot,
				Name=player.DisplayName,
			}
		end

	end
end)
-- function FullUserInfo(userId,callBack)
-- 	if not userId then
-- 		callBack("","")
-- 	end
-- 	if not PlayerInfo[userId] then
-- 		local headShot,nickName,isReady
-- 		local s=pcall(function()
-- 			headShot, isReady = game.Players:GetUserThumbnailAsync(userId, Enum.ThumbnailType.HeadShot,Enum.ThumbnailSize.Size420x420)
-- 			nickName=game.Players:GetNameFromUserIdAsync(userId)
-- 			if headShot and nickName then
-- 				PlayerInfo[userId]={
-- 					Head=headShot,
-- 					Name=nickName,
-- 				}
-- 			end

-- 		end)
		
-- 		if callBack then
-- 			if not headShot then
-- 				headShot=""
-- 			end

-- 			if not nickName then
-- 				nickName=tostring(userId)
-- 			end

-- 			callBack(headShot,nickName)
-- 		end
-- 	else

-- 		if callBack then
-- 			local headShot=PlayerInfo[userId].Head
-- 			local nickName=PlayerInfo[userId].Name

-- 			if not headShot then
-- 				headShot=""
-- 			end

-- 			if not nickName then
-- 				nickName=tostring(userId)
-- 			end
			
-- 			callBack(headShot,nickName)
-- 		end
-- 	end
-- end

local M={}

function M.Update()
	-- print("---UUUUUU-----Update-BBB--->")
	UpdateConnect = coroutine.wrap(function()
		while true do
			-- print("---UUUUUU--HeadImage-Name--loop--->")
			local kId=nil
			for k,v in pairs(CallBackList) do
				local headShot, isRead,nickName
				local s,e=xpcall(function()
					 headShot, isRead= game.Players:GetUserThumbnailAsync(k, Enum.ThumbnailType.HeadShot,Enum.ThumbnailSize.Size420x420)
					 nickName =game.Players:GetNameFromUserIdAsync(k)
				end,debug.traceback)

				if headShot and nickName then
					PlayerInfo[k]={
						Head=headShot,
						Name=nickName,
					}
				end

				if not headShot then
					headShot=""
				end
				if not nickName then
					nickName="userId:"..k
				end

				for _,fuc in pairs(v) do
					local s1,e1=xpcall(function()
						fuc(headShot,nickName)
				   end,debug.traceback)
				end

				kId=k
				break
			end
			if kId then
				CallBackList[kId]=nil
				task.wait(0.2)
			else
				-- coroutine.close(UpdateConnect)
				UpdateConnect=nil
				return
			end
		end
	end)

	UpdateConnect()

end
function M.GetPlayerInfo(userId,bk)
	assert(userId,"userId==null")
	if PlayerInfo[userId] then
		if bk then
			local headShot=PlayerInfo[userId].Head
			local nickName=PlayerInfo[userId].Name

			if not headShot then
				headShot=""
			end
			if not nickName then
				nickName=tostring(userId)
			end

			bk(headShot,nickName)
		end
	
	else
		if not CallBackList[userId] then
			CallBackList[userId]={}
		end
		if bk then
			table.insert(CallBackList[userId],bk)
		end
		if not UpdateConnect then
			M.Update()
		end
		
	end
end




return M